package org.apache.nifi.processors.huawei.dli;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.dli.v1.DliClient;
import com.huaweicloud.sdk.dli.v1.region.DliRegion;
import org.apache.nifi.processor.ProcessContext;

import java.util.List;

import static org.apache.nifi.processors.huawei.common.PropertyDescriptors.*;
import static org.apache.nifi.processors.huawei.dli.DLICreateSqlJob.DLI_PROJECT_ID;

public class DLIUtils {

    public static DliClient createClient(final ProcessContext processContext) {

        final String accessKey = processContext.getProperty(ACCESS_KEY).evaluateAttributeExpressions().getValue();
        final String secretKey = processContext.getProperty(SECRET_KEY).evaluateAttributeExpressions().getValue();
        final String projectId = processContext.getProperty(DLI_PROJECT_ID).evaluateAttributeExpressions().getValue();
        final String region = processContext.getProperty(DLI_REGION).evaluateAttributeExpressions().getValue();

        return DliClient.newBuilder().withCredential(
                new BasicCredentials().withAk(accessKey).withSk(secretKey).withProjectId(projectId))
                .withRegion(DliRegion.valueOf(region)).build();
    }

    public static List<Region> getAvailableRegions() {

        return List.of(
                DliRegion.AF_SOUTH_1,
                DliRegion.AP_SOUTHEAST_1,
                DliRegion.AP_SOUTHEAST_2,
                DliRegion.AP_SOUTHEAST_3,
                DliRegion.CN_EAST_2,
                DliRegion.CN_EAST_3,
                DliRegion.CN_NORTH_1,
                DliRegion.CN_NORTH_2,
                DliRegion.CN_NORTH_4,
                DliRegion.CN_NORTH_9,
                DliRegion.CN_SOUTH_1,
                DliRegion.CN_SOUTHWEST_2,
                DliRegion.LA_NORTH_2,
                DliRegion.LA_SOUTH_2,
                DliRegion.NA_MEXICO_1,
                DliRegion.RU_NORTHWEST_2,
                DliRegion.SA_BRAZIL_1
        );
    }
}
